package util.BReceiverUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;

import org.greenrobot.eventbus.EventBus;

import util.HomeUtil;
import util.LogUtil;
import util.NetUtils;

/**
 * Created by liuxiaobin on 2016/12/15
 */

public class NetWorkstateReceiver extends BroadcastReceiver {
    private String action = null;
    @Override
    public void onReceive(Context context, Intent intent) {
        //检测API是不是小于21，因为到了API21之后getNetworkInfo(int networkType)方法被弃用
        action = intent.getAction();
        LogUtil.eE("ReceiveAction",".......action........"+action);
        if (Intent.ACTION_SCREEN_ON.equals(action)) { // 屏幕唤醒时
            Intent intent1 = new Intent(context,AlarmReceiver.class);
            intent1.putExtra("networking","updateNetwork6");
            context.sendBroadcast(intent1);
        }else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁
            Intent intent1 = new Intent(context,AlarmReceiver.class);
            intent1.putExtra("networking","updateNetwork6");
            context.sendBroadcast(intent1);
        }else {
            if (android.os.Build.VERSION.SDK_INT  < android.os.Build.VERSION_CODES.LOLLIPOP) {

                //获得ConnectivityManager对象
                ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

                //获取ConnectivityManager对象对应的NetworkInfo对象
                //获取WIFI连接的信息
                NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                //获取移动数据连接的信息
                NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                    showHomeView(context);
                    LogUtil.eE("Context","..........1..........");
                } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
                    showHomeView(context);
                    LogUtil.eE("Context","..........2..........");
                } else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                    LogUtil.eE("Context","..........3..........");
                } else {
                    LogUtil.eE("Context","..........4..........");
                }
            } else {
                //这里的就不写了，前面有写，大同小异
                System.out.println("API level 大于21");
                //获得ConnectivityManager对象
                ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                //获取所有网络连接的信息
                Network[] networks = connMgr.getAllNetworks();
                //用于存放网络连接信息
                StringBuilder sb = new StringBuilder();
                //通过循环将网络信息逐个取出来
                for (int i = 0; i < networks.length; i++) {
                    //获取ConnectivityManager对象对应的NetworkInfo对象
                    NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                    NetworkInfo.State state = networkInfo.getState();
                    boolean isConnected = state == NetworkInfo.State.CONNECTED; // 当然，这样可以更精确的确定状态
                    if(networkInfo !=null){
                        if(isConnected){
                            showHomeView(context);
                        }
                        LogUtil.eE("","..........6.........."+isConnected);
                    }
                    LogUtil.eE("Context","..........7..........");
                }
            }
        }
    }


    /**
     * 更新手机UI 因为一换网络 上面的会执行多次
     */
    private void showHomeView(final Context context){
        LogUtil.eE("","....showHomeView..."+NetUtils.getInstance(context).getNetWorkTeyt());
            if(NetUtils.getInstance(context).getNetWorkTeyt() == 1){
                if(isWifi(context)){
                    HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if(NetUtils.getInstance(context).getNetWorkTeyt() == 1){
                                    LogUtil.eE("Context","showHomeView。。。。2"+"..."+NetUtils.getInstance(context).getNetWorkTeyt()+"...isWifi..."+isWifi(context));
                                    if(isWifi(context)){HomeUtil.getHemeUtilNew().setWiFiView(context);}
                            }else if(NetUtils.getInstance(context).getNetWorkTeyt() == 0){
                                HomeUtil.getHemeUtilNew().setWiFiView(context);
                            }
                        }
                    },2000);
                }
            }else if(NetUtils.getInstance(context).getNetWorkTeyt() == 0){
                HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(NetUtils.getInstance(context).getNetWorkTeyt() == 0){
                            LogUtil.eE("Context","showHomeView。。。。3"+"..."+NetUtils.getInstance(context).getNetWorkTeyt()+"...isWifi..."+isWifi(context));
                            if(!isWifi(context)){HomeUtil.getHemeUtilNew().setWiFiView(context);}
                        }else if(NetUtils.getInstance(context).getNetWorkTeyt() == 0){
                            HomeUtil.getHemeUtilNew().setWiFiView(context);
                        }
                    }
                },2000);
            }
    }

    /**
     * 判断wifi是否连接
     */
    private static boolean isWifi(Context mContext) {
        ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }

}
